<?php
namespace app\admin\controller;

use app\common\cache\common\IAreaCache;
use app\common\controller\AdminBaseController;
use app\common\enum\IsDelete;
use app\common\ext\IDb;
use app\common\ext\IRequest;
use think\Db;

class LoanApply extends AdminBaseController{
    /**
     * @功能：贷款申请列表
     * @开发者：wdd
     * @return string
     */
    public function loan_apply_list(){
        //接收参数
        $loan_apply_id = IRequest::get("loan_apply_id");
        $user_id = IRequest::get("user_id");
        $loan_product_id = IRequest::get("loan_product_id");
        $loan_product_name = IRequest::get("loan_product_name");
        $real = IRequest::get("real");
        $nick = IRequest::get("nick");
        $phone = IRequest::get("phone");
        $area = IRequest::get('area');

        //整理区域数据
        $province_id = 0;
        $city_id = 0;
        if(!empty($area) && is_array($area)){
            if(!empty($area[0])) $province_id = $area[0]; //取得省份
            if(!empty($area[1])) $city_id = $area[1]; //取得城市
        }

        $loan_apply_where = null;
        if (!empty($loan_apply_id)) $loan_apply_where['la.loan_apply_id'] = $loan_apply_id;
        if (!empty($user_id)) $loan_apply_where['la.user_id'] = $user_id;
        if (!empty($loan_product_id)) $loan_apply_where['la.loan_product_id'] = $loan_product_id;
        if (!empty($loan_product_name)) $loan_apply_where['lp.loan_product_name'] = array("like","%{$loan_product_name}%");
        if (!empty($real)) $loan_apply_where['us.real'] = array("like","%{$real}%");
        if (!empty($nick)) $loan_apply_where['us.nick'] = array("like","%{$nick}%");
        if (!empty($phone)) $loan_apply_where['us.phone'] = $phone;
        if (!empty($province_id)) $loan_apply_where['la.province_id'] = $province_id;
        if (!empty($city_id)) $loan_apply_where['la.city_id'] = $city_id;
        $loan_apply_list = IDb::getInstance('loan_apply as la')
            ->setDbFiled('la.*,us.nick,us.phone,us.real,lp.loan_product_name')
            ->setDbJoin('loan_product as lp','lp.loan_product_id=la.loan_product_id')
            ->setDbJoin('user as us','us.user_id=la.user_id')
            ->setDbWhere($loan_apply_where)
            ->setDbOrder('la.loan_apply_id desc')
            ->pag();
        if ($loan_apply_list === false){
            error('获取贷款申请列表失败');
        }

        //取得取得区域信息
        $area_list = IAreaCache::getAllList(2);

        //页面赋值
        $this->assign("loan_apply_id",$loan_apply_id);
        $this->assign("user_id",$user_id);
        $this->assign("loan_product_id",$loan_product_id);
        $this->assign("loan_product_name",$loan_product_name);
        $this->assign("real",$real);
        $this->assign("nick",$nick);
        $this->assign("phone",$phone);
        $this->assign("user_id",$user_id);
        $this->assign("loan_apply_list",$loan_apply_list);
        $this->assign("area",$area);
        $this->assign("area_list",$area_list);

        //渲染模板输出
        return view();
    }
    /**
     * @功能：贷款申请详情
     * @开发者：cxl
     * @return string
     */
    public function loan_apply_info(){
        //接收参数
        $loan_apply_id = IRequest::get("loan_apply_id",IRequest::NOT_EMPTY,"贷款申请编号不能为空！");

        //取得日志
        $loan_apply_where['la.loan_apply_id'] = $loan_apply_id;
        $loan_apply_info = IDb::getInstance('loan_apply as la')
            ->setDbFiled('la.*,us.nick,us.phone,us.real,lp.loan_product_name')
            ->setDbJoin('loan_product as lp','lp.loan_product_id=la.loan_product_id')
            ->setDbJoin('user as us','us.user_id=la.user_id')
            ->setDbWhere($loan_apply_where)
            ->row();
        if($loan_apply_info === false){
            error("查找不到申请记录！");
        }

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("loan_apply_info",$loan_apply_info);

        //渲染模板输出
        return view();
    }
    /**
     * @功能：贷款申请详情
     * @开发者：cxl
     * @return string
     */
    public function loan_apply_log(){
        //接收参数
        $loan_apply_id = IRequest::get("loan_apply_id",IRequest::NOT_EMPTY,"贷款申请编号不能为空！");

        $where['loan_apply_id'] = $loan_apply_id;
        $loan_apply_log = IDb::getInstance('loan_apply_log')->setDbWhere($where)->pag();
        if ($loan_apply_log === false){
            error('获取贷款申请详情失败');
        }

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("loan_apply_log",$loan_apply_log);

        //渲染模板输出
        return view();
    }
}